package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.controller.form.ChangeGoodsStateForm;
import com.woniu.model.Result;
import com.woniu.form.AddGoodsForm;
import com.woniu.form.ListingGoodsForm;
import com.woniu.form.ModifyGoodsFrom;
import com.woniu.model.Goods;
import com.woniu.params.AddGoodsParam;
import com.woniu.params.ChangeGoodsStateParam;
import com.woniu.params.ListingGoodsParam;
import com.woniu.params.ModifyGoodsParam;
import com.woniu.service.GoodsService;
import com.woniu.utils.FileUtil;
import com.woniu.utils.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.websocket.server.PathParam;
import java.awt.*;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月25日
 */
@RestController
@Validated
@RequestMapping("/goods")
@Api(tags = "商品接口")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 加载所有商品,不分页
     * @return 返回所有商品数据
     */
    @SentinelResource
    @GetMapping("/loadAll.api")
    @ApiOperation("加载所有商品,不分页")
    public Result<List<Goods>> loadAll(){
        List<Goods> list = goodsService.list();
        return Result.newListInstance(Goods.class).setData(list).setMessage("查询所有商品成功(不分页)");
    }

    /**
     * 加载商品,分页(用户使用)
     * @param pageNum 第几页
     * @param pageSize 每页的记录(数据)条数
     * @return
     */
    @GetMapping("/loadAllPage")
    @ApiOperation("加载商品,分页(用户使用)")
    public Result<Page<Goods>> loadAllPage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        //System.out.println("&&&&&&&&&&&&&&&&&&&"+"\t"+pageSize);
        Page<Goods> goodsList = goodsService.loadAllPage(pageNum, pageSize);
        return Result.newPageInstance(Goods.class).setData(goodsList).setCode(200).setMessage("查询商品成功,分页");
    }

    /**
     * 传入一个商品id的集合 删除传入的id集合的商品
     * @param ids 商品id的集合
     * @return
     */
    @PostMapping("/delGoods.api")
    @ApiOperation("根据ids删除商品")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "ids", value = "商品ID列表", paramType = "body", dataType = "List", required = true)
//    })
    public Result<List<Goods>> delGoods(@ApiParam(name = "ids",value = "要删除的商品id集合") @RequestBody
                                            @NotNull(message = "商品集合不能为空")
                                            List<Integer> ids){
        return Result.newListInstance(Goods.class).setData(goodsService.delGoods(ids)).setMessage("根据ids删除商品成功");
    }

    /**
     * 根据id删除商品
     * @param goodsId 商品id
     * @return
     */
    @GetMapping("/del")
    @ApiOperation("根据id删除商品")
    public Result<?> del(Integer goodsId){
        goodsService.del(goodsId);
        return Result.newInstance(Goods.class).setCode(200).setMessage("根据id删除商品成功");
    }

    /**
     * 修改商品信息 要先下架后才能修改
     * @param from
     * @return
     */
    @PostMapping("/modifyGoods.api")
    @ApiOperation("修改商品信息")
    public Result<Goods> modifyGoods(@RequestBody @Validated ModifyGoodsFrom from){
        Goods goods = goodsService.modifyGoods(BeanUtil.toBean(from, ModifyGoodsParam.class));

        return Result.newInstance(Goods.class).setData(goods).setMessage("修改商品信息成功");
    }

    /**
     * 上架商品 上架不是新增
     * @param form
     * @return
     */
    @PostMapping("/listing")
    @ApiOperation("上架商品")
    public Result<List<Goods>> listing(@RequestBody @Validated List<ListingGoodsForm> form) throws Exception {
        List<Goods> goods = goodsService.listing(BeanUtil.copyToList(form, ListingGoodsParam.class));
        return Result.newListInstance(Goods.class).setData(goods).setMessage("上架商品成功");
    }

    /**
     * 下架商品
     * @param id 传入一个商品id
     * @return
     */
    @GetMapping("/offShelf/{id}")
    @ApiOperation("下架商品")
    public Result<Goods> offShelf(@ApiParam(name = "id",value = "商品id") @PathVariable("id")
                                      @NotNull(message = "商品id不能为空")
                                      @Positive(message = "商品id必须是正整数")
                                      Integer id){
        return Result.newInstance(Goods.class).setMessage("下架商品成功").setData(goodsService.offShelf(id));
    }

    /**
     * 上传图片
     * @param image
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadImg")
    @ApiOperation("上传图片")
    public Result<String> uploadImg(@NotNull(message = "图片不能为空") MultipartFile image) throws Exception {
        String filePath =  goodsService.uploadImg(image);
        return Result.newInstance(String.class).setData(filePath).setCode(200);
    }

    /**
     * 上传图片成功后,修改商品的图片地址
     * @param id 商品id
     *        img 图片地址
     * @return
     */
    @GetMapping("/changePictor")
    @ApiOperation("修改图片")
    public Result<?> changePictor(@RequestParam("id") Integer id,@RequestParam("img") String img){
        goodsService.changePictor(id,img);
        return Result.newInstance(null).setMessage("修改图片地址成功").setCode(200);
    }


    /**
     * 新增商品
     * @param form
     * @return
     */
    @PostMapping("/addGoods")
    @ApiOperation("新增商品")
    public Result<Goods> addGoods(@RequestBody @Validated AddGoodsForm form) throws Exception {
        Goods goods =  goodsService.addGoods(BeanUtil.toBean(form, AddGoodsParam.class));
        return Result.newInstance(Goods.class).setData(goods).setMessage("新增商品成功");
    }

    /**
     * 根据id查询商品信息
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/goodsById/{id}")
    @ApiOperation("根据id查询商品信息")
    public Result<Goods> goodsById(@ApiParam(name = "id",value = "商品id") @PathVariable("id")
                                       @NotNull(message = "商品id不能为空")
                                       @Positive(message = "商品id必须是正整数")
                                       Integer id){
        return Result.newInstance(Goods.class).setMessage("根据id查询商品成功").setData(goodsService.getById(id));
    }

    /**
     * 根据商品的集合查询 商品信息
     * @param ids 商品id集合
     * @return 返回商品信息集合
     */
    @PostMapping("/goodsByList")
    @ApiOperation("根据商品的集合查询商品信息")
    public Result<List<Goods>> goodsByList(@ApiParam(name = "ids",value = "商品集合") @NotNull(message = "商品id集合不能为空") @RequestBody Set<Integer> ids){
        return Result.newListInstance(Goods.class).setData(goodsService.listByIds(ids)).setMessage("根据ids查询商品数据成功").setCode(200);
    }


    /**
     * 修改商品状态
     * @param form 更改商品状态Form
     * @return 返回商品信息集合
     */
    @PostMapping("/changeGoodsState")
    @ApiOperation("更改商品状态")
    public Result<?> changeMedicineState(@RequestBody ChangeGoodsStateForm form){
        goodsService.changeGoodsState(BeanUtil.toBean(form, ChangeGoodsStateParam.class));
        return Result.newInstance(Goods.class).setMessage("根据id查询商品成功").setCode(200);
    }

    /**
     * 根据名字模糊查询商品
     * @param name 商品名称
     * @return 返回商品信息集合
     */
    @GetMapping("/loadGoodsByName")
    @ApiOperation("根据名字模糊查询商品")
    public Result<Page<Goods>> loadGoodsByName(@RequestParam("name") String name,@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize){
        Page<Goods> page = goodsService.loadGoodsByName(name,pageNum,pageSize);
        return Result.newPageInstance(Goods.class).setData(page).setCode(200).setMessage("根据名字模糊查询药品成功");
    }
}

